package com.iwish.server.model;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import com.iwish.server.util.Base64Util;
import com.iwish.server.util.RequestCode;

public class RequestDispatcher {
	Logger logger = Logger.getLogger(RequestDispatcher.class);
	
	private String request_code;
	private String[] arr;
	
	public RequestDispatcher()
	{
		
	}
	
	public RequestDispatcher(String code, String[] array)
	{
		request_code = code;
		arr = array;
	}
	
	public boolean dispatch(IoSession session)
	{
		
		if(RequestCode.LOGIN.equals(request_code))
		{
			System.out.println("Login process");
			//String email = new String(Base64Util.decode(arr[1]));
			//String password = new String(Base64Util.decode(arr[2]));
			
			//still base64 code , email and password
			String email = new String(Base64Util.decode(arr[1]));
			String password = new String(Base64Util.decode(arr[2]));
			
			LoginService ls = new LoginService(email,password);
			//Obj.User.Builder user = Obj.User.newBuilder();
			if(ls.login(session))
			{
				return true;
			}
			else
			{
				return false;
			}
			
		}
		else if(RequestCode.UPDATE.equals(request_code))
		{
			System.out.println("Update process");
			System.out.println("RequestDispatcher - Update process");
			if(arr.length != 8)
			{
				return false;
			}
			
			//TODO: write a safe parser
			double x = Double.parseDouble(new String(Base64Util.decode(arr[1])));
			double y = Double.parseDouble(new String(Base64Util.decode(arr[2])));
			double z = Double.parseDouble(new String(Base64Util.decode(arr[3])));
			String uid = new String(Base64Util.decode(arr[4]));
			int page_num = Integer.parseInt(new String(Base64Util.decode(arr[5])));
			String filter_type = new String(Base64Util.decode(arr[6]));
			String custom_filter = arr[7];
			
			UpdateService us = new UpdateService(x, y, z, 
					uid, page_num, filter_type, custom_filter);
			us.update(session);
			
		}
		else if(RequestCode.LOGIN_SINA_ACCOUNT.equals(request_code))
		{
			logger.debug("Login with sina account process");
			if(arr.length != 2)
			{
				logger.debug("request params invalid");
				return false;
			}
			
			String sina_account = new String(Base64Util.decode(arr[1]));
			LoginWithSinaAccountService lsas = new LoginWithSinaAccountService(sina_account);
			if(!lsas.login(session))
			{
				logger.debug("This sina account is new");
				return false;
			}
			return true;
		}
		else if(RequestCode.REGISTER_SINA_ACCOUNT.equals(request_code))
		{
			logger.debug("Register with sina account process");
			if(arr.length != 6)
			{
				logger.debug("request params invalid");
				return false;
			}
			String sina_account = new String(Base64Util.decode(arr[1]));
			String img = arr[2];
			String nickname = new String(Base64Util.decode(arr[3]));
			String gender = new String(Base64Util.decode(arr[4]));
			String date_of_birth = new String(Base64Util.decode(arr[5])); 
			
			boolean genderX = true;
			if("0".equals(gender)) genderX = false;
			
			RegisterWithSinaAccountService rsas = new RegisterWithSinaAccountService(sina_account
					, img, nickname, genderX, date_of_birth);
			if(!rsas.register(session))
			{
				logger.debug("regist with sina account failed");
				return false;
			}
			return true;
		}
		else if(RequestCode.REGISTER.equals(request_code))
		{
			System.out.println("Register process");
			if (arr.length != 3)
				return false;
			
			String email = new String(Base64Util.decode(arr[1]));
			String password = new String(Base64Util.decode(arr[2]));
//			String nickname = new String(Base64Util.decode(arr[3]));
//			String gender = new String(Base64Util.decode(arr[4]));
//			String img = arr[5]; //keep img in base64 string
			
//			RegisterService rs = new RegisterService(email, password, nickname, gender, img);
			RegisterService rs = new RegisterService(email, password);
			if(!rs.register(session))
			{
				System.out.println("new user, can regist");
				return false;
			}
			System.out.println("user exists, cannot regist");
			return true;
		}
		else if(RequestCode.ACT_DETAIL.equals(request_code))
		{
			logger.debug("Activity detail process");
			if(arr.length != 3)
			{
				return false;
			}
			
			String uid = new String(Base64Util.decode(arr[1]));
			String aid = new String(Base64Util.decode(arr[2]));
			
			ActDetailService ads = new ActDetailService(uid, aid);
			if(!ads.search(session))
			{
				return false;
			}
			return true;
		} 
		//A7: Activity Comment
		else if (RequestCode.ACT_COMMENT.equals(request_code)) {
			logger.debug("Act comment process");
			if (arr.length != 4) {
				return false;
			}
			String aid = new String(Base64Util.decode(arr[1]));
			String uid = new String(Base64Util.decode(arr[2]));
			String comment_detail = new String(Base64Util.decode(arr[3]));

			ActCommentService acs = new ActCommentService(aid, uid,
					comment_detail);
			if (!acs.post(session)) {
				return false;
			}
			return true;
		}
		//A9: Sign Activity
		else if(RequestCode.ACT_SIGN.equals(request_code))
		{
			logger.debug("Activity sign process");
			if(arr.length != 3)
			{
				return false;
			}
			String aid = new String(Base64Util.decode(arr[1]));
			String uid = new String(Base64Util.decode(arr[2]));
			
			ActSignService ass = new ActSignService(aid, uid);
			if(! ass.sign(session))
			{
				return false;
			}
			return true;
		}
		//C1: Search Friend
		else if(RequestCode.SEARCH_FRIEND.equals(request_code))
		{
			
		}
		//C3: Add friend
		else if(RequestCode.ADD_FRIEND.equals(request_code))
		{
			logger.debug("add friend process");
			if(arr.length != 3)
			{
				return false;
			}
			String uid = new String(Base64Util.decode(arr[1]));
			String uid_friend = new String(Base64Util.decode(arr[2]));
			
			CDBFriendService afs = new CDBFriendService(uid , uid_friend);
			if(!afs.add(session))
			{
				return false;
			}
			return true;
		}
		//C5: delete friend
		else if(RequestCode.DELETE_FRIEND.equals(request_code))
		{
			logger.debug("delete friend process");
			if(arr.length != 3)
			{
				return false;
			}
			String uid = new String(Base64Util.decode(arr[1]));
			String uid_friend = new String(Base64Util.decode(arr[2]));
			
			CDBFriendService afs = new CDBFriendService(uid , uid_friend);
			
			if(!afs.delete(session))
			{
				return false;
			}
			return true;
			
		}
		//C7: Block friend
		else if(RequestCode.BLOCK_FRIEND.equals(request_code))
		{
			logger.debug("block friend process");
			if(arr.length != 3)
			{
				return false;
			}
			String uid = new String(Base64Util.decode(arr[1]));
			String uid_friend = new String(Base64Util.decode(arr[2]));
			
			CDBFriendService afs = new CDBFriendService(uid , uid_friend);
			
			if(!afs.block(session))
			{
				return false;
			}
			return true;
		}
		//C9 : Cancel Block friend
		else if(RequestCode.CANCEL_BLOCK_FRIEND.equals(request_code))
		{
			logger.debug("cancel block friend process");
			if(arr.length != 3)
			{
				return false;
			}
			String uid = new String(Base64Util.decode(arr[1]));
			String uid_friend = new String(Base64Util.decode(arr[2]));
			
			CDBFriendService afs = new CDBFriendService(uid , uid_friend);
			
			if(!afs.cancel_block(session))
			{
				return false;
			}
			return true;
		}
		//Update user info request
		else if(RequestCode.UPDATE_USER_INFO.equals(request_code))
		{
			logger.debug("update user info process");
			if(arr.length != 6)
			{
				return false;
			}
			String uid = new String(Base64Util.decode(arr[1]));
			String nickname = new String(Base64Util.decode(arr[2]));
			String gender = new String(Base64Util.decode(arr[3]));
			String date_of_birth = new String(Base64Util.decode(arr[4]));
			String img = arr[5]; //keep img in base64 string
			
			boolean g = true;
			if(gender.equals("0")) g = false;
			
			UpdateUserInfoService uuis = new UpdateUserInfoService(uid, nickname, g, date_of_birth, img);
			if(!uuis.update(session))
			{
				logger.info("update user info fail");
				return false;
			}
			logger.info("update user info succ");
			return true;
			
		}
		else
		{
			
		}
		return true;
	}

}
